package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.crm.domain.pojo.WfActionInfo;
import com.yu.crm.domain.pojo.WfCombiCondition;
import com.yu.crm.domain.pojo.WfCombiInfo;
import com.yu.crm.domain.request.ActionConfReq;
import com.yu.crm.domain.request.ConditionInfoConfReq;
import com.yu.crm.domain.response.ActionConfRep;
import com.yu.crm.domain.response.NodeActionConfInfo;
import com.yu.crm.mapper.WfActionInfoMapper;
import com.yu.crm.service.WfActionInfoService;
import com.yu.crm.service.WfCombiConditionService;
import com.yu.crm.service.WfCombiInfoService;
import com.yxzz.common.exception.GeneralException;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.DateHelpers;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 工作流程表 服务实现类
 * </p>
 *
 * @author donnie-bin
 * @since 2019-10-08
 */
@Log4j2
@Service
public class WfActionInfoServiceImpl extends ServiceImpl<WfActionInfoMapper, WfActionInfo> implements WfActionInfoService {

    @Autowired
    private WfCombiInfoService wfCombiInfoService;
    @Autowired
    private WfCombiConditionService wfCombiConditionService;
    /**
     * 通过actionId和nodeId获取statusId
     */
    @Override
    public Integer getStatus(Long combiId, String nodeId, int actionId) {
        QueryWrapper<WfActionInfo> queryWrapper = new QueryWrapper<>();
        if (DataHelpers.isNotNull(combiId)) {
            queryWrapper.eq("combi_id",combiId);
        }
        queryWrapper.eq("action_enum_id",actionId);
        queryWrapper.eq("p_node_id",nodeId);
        queryWrapper.eq("is_deleted",0);
        WfActionInfo wfActionInfo = baseMapper.selectOne(queryWrapper);
        if (DataHelpers.isEmpty(wfActionInfo)) {
            throw new GeneralException("该条记录不存在");
        }
        return wfActionInfo.getStatusEnumId();
    }

    /**
     * 保存节点和动作信息
     */
    @Override
    public void saveActionList(NodeActionConfInfo req) {
        //先做删除再做添加
        String appId = req.getAppId();
        //节点动作表
        del(appId);
        //条件信息表
        wfCombiInfoService.del(appId);
        //条件组合表
        wfCombiConditionService.del(appId);
        //再新增新提交的消息
        List<ActionConfReq> actionInfoList = req.getActionInfoList();
        log.info("动作信息：{}"+actionInfoList);
        List<WfActionInfo> list = new ArrayList<>();
        if (DataHelpers.isNotEmpty(actionInfoList) && actionInfoList.size()>0) {
            for (ActionConfReq actionConfReq:actionInfoList
                 ) {
                WfCombiInfo wfCombiInfo = new WfCombiInfo();
              //WfCombiCondition表信息
                List<ConditionInfoConfReq> conditionInfoConfReqList = actionConfReq.getConditionInfoConfReqList();

                if (DataHelpers.isNotEmpty(conditionInfoConfReqList) && conditionInfoConfReqList.size()>0) {
                    //新增条件关联信息WfCombiInfo表信息
                    wfCombiInfo.setAppId(appId);
                    wfCombiInfo.setGmtCreate(DateHelpers.now());
                    if (DataHelpers.isEmptyString(actionConfReq.getConditionRelationValue())) {
                        wfCombiInfo.setCombiName("0");
                    }else {
                        wfCombiInfo.setCombiName(actionConfReq.getConditionRelationValue());
                    }
                    wfCombiInfoService.save(wfCombiInfo);

                    //新增条件信息
                    List<WfCombiCondition> wfCombiConditions = new ArrayList<>();
                    for (ConditionInfoConfReq confReq:conditionInfoConfReqList
                         ) {
                        WfCombiCondition wfCombiCondition = new WfCombiCondition();
                        wfCombiCondition.setCombiId(wfCombiInfo.getId());
                        wfCombiCondition.setConditionId(confReq.getConditionEnumId());
                        wfCombiCondition.setConditionSymbol(confReq.getConditionType());
                        wfCombiCondition.setConditionValue(confReq.getConditionValue());
                        wfCombiCondition.setGmtCreate(DateHelpers.now());
                        wfCombiCondition.setAppId(appId);
                        wfCombiCondition.setIsDeleted(0);
                        wfCombiConditions.add(wfCombiCondition);
                    }
                    if (wfCombiConditions.size()>0) {
                        wfCombiConditionService.saveBatch(wfCombiConditions);
                    }
                }
                WfActionInfo wfActionInfo = new WfActionInfo();
                BeanUtils.copyProperties(actionConfReq,wfActionInfo);
                //设置条件
                if (DataHelpers.isNotEmpty(wfCombiInfo.getId())) {
                    wfActionInfo.setCombiId(wfCombiInfo.getId());
                }
                wfActionInfo.setAppId(appId);
                wfActionInfo.setIsDeleted(0);
                wfActionInfo.setGmtCreate(DateHelpers.now());
                list.add(wfActionInfo);
            }
            if (list.size()>0) {
                this.saveBatch(list);
            }
        }
    }


    /**
     * 获取条件信息
     * @param pnodeId
     * @param nodeId
     * @param appId
     */
    @Override
    public ActionConfRep getCondition(String pnodeId, String nodeId, String appId) {
        QueryWrapper<WfActionInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_id",appId);
        queryWrapper.eq("p_node_id",pnodeId);
        queryWrapper.eq("node_id",nodeId);
        WfActionInfo wfActionInfo = baseMapper.selectOne(queryWrapper);
        if (DataHelpers.isEmpty(wfActionInfo)) {
            throw new GeneralException("该信息不存在");
        }
        //获取条件id
        Long combiId = wfActionInfo.getCombiId();
        ActionConfRep combiCondition = wfCombiConditionService.getCombiCondition(combiId, appId);
        combiCondition.setStatusEnumId(wfActionInfo.getStatusEnumId());
        combiCondition.setActionEnumId(wfActionInfo.getActionEnumId());
        combiCondition.setCombiId(combiId);
        return combiCondition;

    }

    /**
     * 通过appId做删除
     * @param appId
     */
    public void del(String appId) {
        QueryWrapper<WfActionInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_id",appId);
        baseMapper.delete(queryWrapper);
    }


    /**
     * 通过父节点id和应用id查询Action集合
     * @param appId
     * @param pNodeId
     * @return
     */
    @Override
    public List<WfActionInfo>  listByPid(String appId, String pNodeId) {
        QueryWrapper<WfActionInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_id",appId);
        queryWrapper.eq("p_node_id",pNodeId);
        queryWrapper.eq("is_deleted",0);
        return baseMapper.selectList(queryWrapper);
    }
}
